from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.db.models import Q
from glowdrop import settings
from glowdrop.notes.models import Note
from glowdrop.utils import adjust_avatar
from glowdrop.message.models import Message
from glowdrop.utils.db_util import util_db_select_count

@login_required
def my_space(request):
    """ space login required
    """
    avatar_link = request.user.get_profile().image_link
    if not avatar_link:
        avatar_link = settings.DEFAULT_AVATAR_LINK 
    else:
        avatar_link = avatar_link.url
    user = dict(request.user.__dict__, avatar_link=avatar_link, 
            is_authenticated=True)
    fans_request_num = get_fans_request_number(user['id'])
    friends_request_num = get_friends_request_number(user['id'])
    return render_to_response('member/my_space.html', 
            {'notes': [], 'user': user,
            'friends_request_num':friends_request_num,
            'fans_request_num':fans_request_num
            })

def space(request, username):
    """A user's space
    """
    host = User.objects.get(username=username)
    avatar_link = host.get_profile().image_link
    notes_cnt = host.note_set.count()
    places_ever_go_cnt = 0
    friends_cnt = 0
    if not avatar_link:
        avatar_link = settings.DEFAULT_AVATAR_LINK 
    else:
        avatar_link = avatar_link.url
    host = dict(host.__dict__, avatar_link=avatar_link, 
            bio=host.get_profile().bio, notes_cnt=notes_cnt, 
            places_ever_go_cnt=places_ever_go_cnt, 
            friends_cnt=friends_cnt)
    return render_to_response('member/space.html', 
            {'notes': [], 'host':host, 'user': request.user})

@login_required
def around(request):
    """get one's nearby message
    """
    return HttpResponse('this is around')

@login_required
def around_people(request):
    """get one's nearyby people
    """
    return HttpResponse('this is around_people')

@login_required
def universal(request):
    """get the universal message, the url arguments 'page_no' is required
    """
    page_no = int(request.GET.get('page_no', 0))
    return HttpResponse('this is universal page_no %d' % page_no)

@login_required
def my_message(request):
    user = dict(username=request.user.username, 
            avatar_link=adjust_avatar(request.user.get_profile().image_link),
            is_authenticated=True)
    page_no = int(request.GET.get('page_no', 0))
    page_size = int(request.GET['page_size'])
    type = request.GET.get('type', 'all')
    if type == 'received':
        msg_list = Message.objects.filter(dest=request.user)
    elif type == 'sent':
        msg_list = Message.objects.filter(src=request.user)
    elif type == 'all':
        msg_list = Message.objects.filter(Q(src=request.user)|Q(dest=request.user))
    else:
        msg_list = []
    
    total_page_no = ((msg_list.count()-1)/int(page_size)) + 1;
    msg_list = msg_list[page_no*page_size:(page_no+1)*page_size]
    ret = []
    for m in msg_list:
        ret.append({'id': m.id, 'content': m.content, 
            'src': [m.src.username, adjust_avatar(m.src.get_profile().image_link)], 
            'dest': [m.dest.username, adjust_avatar(m.dest.get_profile().image_link)],
            'pub_date': m.pub_date})

    return render_to_response('member/message.html', 
            {'message_list': ret, 'user': user, 'page_no': page_no, 
                'page_size': page_size, 'total_page_no': total_page_no})


def make_friend(request):
    return HttpResponse("OK...")


def get_fans_request_number(user_id):
    sql = "select count(*) from accounts_friend where uid=%s and sealed=1" %  user_id
    return util_db_select_count(sql)

def get_friends_request_number(user_id):
    sql = "select count(*) from accounts_friend where uid=%s and status=1 and sealed =1" %  user_id
    return util_db_select_count(sql)

@login_required
def my_fans(request):
    return render_to_response('member/my_relationship.html',{'selector':0})

@login_required
def my_friends(request):
    return render_to_response('member/my_relationship.html',{'selector':1})

